﻿
using System;
using System.Collections;
using System.Web.UI.WebControls;
using System.Text;
using System.Web;

namespace Com.Phyfun.Business
{
    #region Walkthrough

    /// <summary>
    /// Walkthrough object for NHibernate mapped table 'Walkthrough'.
    /// </summary>
    public class Walkthrough : BizObject
    {
        #region Member Variables

        protected int _id;
        protected string _walkTitle;
        protected string _walkDesc;
        protected string _flvSrc;
        protected int _duration;
        protected string _youtubeSrc;
        protected bool _flagDeleted;
        protected DateTime _logDateTime;
        protected Game _game;
        protected static String _sortExpression = "Id";
        protected static SortDirection _sortDirection = SortDirection.Ascending;

        #endregion

        #region Constructors

        public Walkthrough() { }

        public Walkthrough(int id) { if (id != 0)ObjectBroker.Load(this, id); }

        public Walkthrough(string walkTitle, string walkDesc, string flvSrc, int duration, string youtubeSrc, bool flagDeleted, DateTime logDateTime, Game game)
        {
            this._walkTitle = walkTitle;
            this._walkDesc = walkDesc;
            this._flvSrc = flvSrc;
            this._duration = duration;
            this._youtubeSrc = youtubeSrc;
            this._flagDeleted = flagDeleted;
            this._logDateTime = logDateTime;
            this._game = game;
        }

        #endregion

        #region Public Properties

        public virtual int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public virtual string WalkTitle
        {
            get { return _walkTitle; }
            set
            {
                if (value != null && value.Length > 200)
                    throw new ArgumentOutOfRangeException("Invalid value for WalkTitle", value, value.ToString());
                _walkTitle = value;
            }
        }

        public virtual string WalkDesc
        {
            get { return _walkDesc; }
            set
            {
                if (value != null && value.Length > int.MaxValue)
                    throw new ArgumentOutOfRangeException("Invalid value for WalkDesc", value, value.ToString());
                _walkDesc = value;
            }
        }

        public virtual string FlvSrc
        {
            get { return _flvSrc; }
            set
            {
                if (value != null && value.Length > 200)
                    throw new ArgumentOutOfRangeException("Invalid value for FlvSrc", value, value.ToString());
                _flvSrc = value;
            }
        }

        public virtual int Duration
        {
            get { return _duration; }
            set
            {
                _duration = value;
            }
        }

        public virtual string YoutubeSrc
        {
            get { return _youtubeSrc; }
            set
            {
                if (value != null && value.Length > 200)
                    throw new ArgumentOutOfRangeException("Invalid value for YoutubeSrc", value, value.ToString());
                _youtubeSrc = value;
            }
        }

        public virtual bool FlagDeleted
        {
            get { return _flagDeleted; }
            set { _flagDeleted = value; }
        }

        public virtual DateTime LogDateTime
        {
            get { return _logDateTime; }
            set { _logDateTime = value; }
        }

        public virtual Game Game
        {
            get { return _game; }
            set { _game = value; }
        }

        public static String SortExpression
        {
            get { return _sortExpression; }
            set { _sortExpression = value; }
        }

        public static SortDirection SortDirection
        {
            get { return _sortDirection; }
            set { _sortDirection = value; }
        }


        public virtual string PageFullPath
        {
            get
            {
                return ApplicationPath + "Walkthroughs/" + PageFileName;
            }
        }

        public virtual string FlvFullPath
        {
            get
            {
                return ApplicationPath + FlvSrc;
            }
        }


        private string ApplicationPath
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(HttpContext.Current.Request.ApplicationPath);
                if (!HttpContext.Current.Request.ApplicationPath.EndsWith("/"))
                {
                    sb.Append("/");
                }
                return sb.ToString();
            }
        }

        public virtual string PageFileName
        {
            get
            {
                string rtn = Id + "-" + WalkTitle.Replace(" ", "-");
                rtn = rtn.Replace("/", "-");
                rtn = rtn.Replace(":", "-");
                rtn = rtn.Replace("'", "-");
                rtn = rtn.Replace("’", "-");
                rtn = rtn.Replace("!", "-");
                rtn = rtn.Replace("?", "-");
                rtn = rtn.Replace("&", "-");
                rtn = rtn.Replace("%", "-");
                rtn = rtn.Replace("---", "-");
                rtn = rtn.Replace("--", "-");
                return rtn;
            }
        }

        public virtual string FormattedDuration
        {
            get
            {
                TimeSpan t = TimeSpan.FromSeconds(Duration);
                return t.ToString();
            }
        }
        #endregion

        #region IComparable Methods
        public virtual int CompareTo(object obj)
        {
            if (!(obj is Walkthrough))
                throw new InvalidCastException("This object is not of type Walkthrough");

            int relativeValue;
            switch (SortExpression)
            {
                case "Id":
                    relativeValue = this.Id.CompareTo(((Walkthrough)obj).Id);
                    break;
                case "WalkTitle":
                    relativeValue = (this.WalkTitle != null) ? this.WalkTitle.CompareTo(((Walkthrough)obj).WalkTitle) : -1;
                    break;
                case "WalkDesc":
                    relativeValue = (this.WalkDesc != null) ? this.WalkDesc.CompareTo(((Walkthrough)obj).WalkDesc) : -1;
                    break;
                case "FlvSrc":
                    relativeValue = (this.FlvSrc != null) ? this.FlvSrc.CompareTo(((Walkthrough)obj).FlvSrc) : -1;
                    break;
                case "YoutubeSrc":
                    relativeValue = (this.YoutubeSrc != null) ? this.YoutubeSrc.CompareTo(((Walkthrough)obj).YoutubeSrc) : -1;
                    break;
                case "FlagDeleted":
                    relativeValue = (this.FlagDeleted != null) ? this.FlagDeleted.CompareTo(((Walkthrough)obj).FlagDeleted) : -1;
                    break;
                case "LogDateTime":
                    relativeValue = (this.LogDateTime != null) ? this.LogDateTime.CompareTo(((Walkthrough)obj).LogDateTime) : -1;
                    break;
                default:
                    goto case "Id";
            }
            if (Walkthrough.SortDirection == SortDirection.Ascending)
                relativeValue *= -1;
            return relativeValue;
        }
        #endregion
    }

    #endregion
}
